package com.duqc.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @ClassName SocketChannelDemo
 * @Description TCP管道 通过TCP读写网络中的数据
 * @Author duqc
 * @Date 2021/8/23
 **/
public class SocketChannelDemo {

    public static void main(String[] args) throws IOException {
        ServerSocketChannel open = ServerSocketChannel.open();
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 6666);
        open.bind(inetSocketAddress);
        ByteBuffer allocate = ByteBuffer.allocate(1024);
        while (true) {
            SocketChannel accept = open.accept();
            while (accept.read(allocate) != -1) {
                System.out.println(new String((allocate.array())));
                allocate.clear();
            }
        }
    }

}
